package com.cvit.cn.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.cn.bean.Msg;
import com.cvit.cn.bean.VendorsQuotation;
import com.cvit.cn.service.VendorsQuotationService;
import com.cvit.cn.test.UuidUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class VendorsQuotationController {
	@Autowired
	VendorsQuotationService vendorsQuotationService;
	@RequestMapping("/a")
	@ResponseBody
	public Msg getAll(){
		List<VendorsQuotation> v=vendorsQuotationService.getAll();
		return Msg.success().add("v", v);
	}
	/**
	 * 报价成功
	 * */
	@RequestMapping("/b")
	@ResponseBody
	public Msg updataSupplierTypeSuccess(@RequestParam("vendorsId")String vendorsId,@RequestParam("releaseId")String releaseId){
		VendorsQuotation vq=new VendorsQuotation();
		vq.setVendorsId(vendorsId);
		vq.setReleaseId(releaseId);
		vq.setSupplierType("1");
		vendorsQuotationService.updataSupplierType(vq);
		return Msg.success();
	}
	/**
	 * 报价失败
	 * */
	@RequestMapping("/c")
	@ResponseBody
	public Msg updataSupplierTypeFail(@RequestParam("vendorsId")String vendorsId,@RequestParam("releaseId")String releaseId){
		VendorsQuotation vq=new VendorsQuotation();
		vq.setVendorsId(vendorsId);
		vq.setReleaseId(releaseId);
		vq.setSupplierType("1");
		vendorsQuotationService.updataSupplierType(vq);
		return Msg.success();
	}
	/**
	 * 报价中
	 * */
	@RequestMapping("/d")
	@ResponseBody
	public Msg updataSupplierTypeIng(@RequestParam("vendorsId")String vendorsId,@RequestParam("releaseId")String releaseId){
		VendorsQuotation vq=new VendorsQuotation();
		vq.setVendorsId(vendorsId);
		vq.setReleaseId(releaseId);
		vq.setSupplierType("1");
		vendorsQuotationService.updataSupplierType(vq);
		return Msg.success();
	}
	/**
	 * 根据supplierType查报价类别
	 * 0：未参加报价
       1：报价成功
       2：报价失败
       3：报价中
	 * */
	@RequestMapping("/e")
	@ResponseBody
	public Msg getSupplierTypeSuccess(@RequestParam("vendorsId")String vendorsId,@RequestParam("releaseId")String releaseId,@RequestParam("supplierType")String supplierType){
		List<VendorsQuotation> vqs=vendorsQuotationService.getSupplierTypeSuccess(vendorsId,releaseId,supplierType);
		return Msg.success().add("vqs", vqs);
	}
	/**
	 * 供应商状态中的所有批物品
	 * */
	@RequestMapping("/f")
	@ResponseBody
	public Msg getSupplierTypeRelease(@RequestParam("vendorsId")String vendorsId,@RequestParam("supplierType")String supplierType){
		List<VendorsQuotation> vqs=vendorsQuotationService.getSupplierTypeRelease(vendorsId,supplierType);
		return Msg.success().add("vqs", vqs);
	}
	/**
	 * 供应商未参加报价的订单
	 * */
	@RequestMapping("/g")
	@ResponseBody
	public Msg getNoSupplierTypeRelease(@RequestParam("vendorsId")String vendorsId){
		if(vendorsId!=""){
		List<VendorsQuotation> vqs=vendorsQuotationService.getNoSupplierTypeRelease(vendorsId);
		return Msg.success().add("vqs", vqs);
		}else{
			return Msg.fail();
		}
	}
	/**
	 * 供应商未参加报价的并进行中的订单
	 * */
	@RequestMapping("/h")
	@ResponseBody
	public Msg getNoIngSupplierTypeRelease(@RequestParam("vendorsId")String vendorsId){
		if(vendorsId!=""){
		List<VendorsQuotation> vqs=vendorsQuotationService.getNoIngSupplierTypeRelease(vendorsId);
		return Msg.success().add("vqs", vqs);
		}else{
			return Msg.fail();
		}
	}
	/**
	 * 批量添加
	 * */
	@RequestMapping(value="/i")
	@ResponseBody
	public Msg addQuotations(String jsonStr) throws Exception{
		//System.out.println("qqqqqqqqqq"+jsonStr);
		//String jsonStr = "[{'releaseId':'180110-0002','commodityId':'1','vendorsId':'1','unitPrice':'100000000','money':'2000000','remarks':'1','total':'500000000'}]";
				//,{'releaseId':'1','commodityId':'1','vendorsId':'2','unitPrice':'100000000','money':'2000000','remarks':'2','total':'500000000'},{'releaseId':'1','commodityId':'1','vendorsId':'3','unitPrice':'100000000','money':'2000000','remarks':'3','total':'500000000'},{'releaseId':'1','commodityId':'1','vendorsId':'4','unitPrice':'100000000','money':'2000000','remarks':'4','total':'500000000'},{'releaseId':'1','commodityId':'1','vendorsId':'5','unitPrice':'100000000','money':'2000000','remarks':'5','total':'500000000'}]";
		if(jsonStr!="" || !jsonStr.equals("")){
			JSONArray jsons = JSONArray.fromObject(jsonStr);
			for(int i=0;i<jsons.size();i++){
				JSONObject json = jsons.getJSONObject(i);
				VendorsQuotation vendorsQuotation=new VendorsQuotation();
				vendorsQuotation.setId(UuidUtil.get32UUID());
				vendorsQuotation.setCommodityId(json.getString("commodityId"));
				vendorsQuotation.setReleaseId(json.getString("releaseId"));
				vendorsQuotation.setVendorsId(json.getString("vendorsId"));
				vendorsQuotation.setUnitPrice(json.getString("unitPrice"));
				vendorsQuotation.setMoney(json.getString("money"));
				vendorsQuotation.setRemarks(json.getString("remarks"));
				vendorsQuotation.setTotal(json.getString("total"));
				vendorsQuotation.setNotes(json.getString("notes"));
				vendorsQuotation.setSupplierType("0");
				vendorsQuotationService.addQuotations(vendorsQuotation);
			}
			return Msg.success();
		}else{
			return Msg.fail().add("fail", "数据为空");
		}
	}
	/**
	 * 根据订单号查询报价的众多供应商
	 * */
	@RequestMapping("/j")
	@ResponseBody
	public Msg getReleaseAll(String releaseId){
		List<VendorsQuotation> r=vendorsQuotationService.getReleaseById(releaseId);
			return Msg.success().add("r", r);
	}
	/**
	 * 查询未处理的物品
	 * */
	@RequestMapping("/k")
	@ResponseBody
	public Msg getSupplierTypeThree(@RequestParam("releaseId")String releaseId){
		List<VendorsQuotation> r=vendorsQuotationService.getSupplierTypeThree(releaseId);
		return Msg.success().add("r", r);
	}
	/**
	 * 查询处理成功的物品
	 * */
	@RequestMapping("/l")
	@ResponseBody
	public Msg getSupplierTypeOne(@RequestParam("releaseId")String releaseId){
		List<VendorsQuotation> r=vendorsQuotationService.getSupplierTypeOne(releaseId);
		return Msg.success().add("r", r);
	}
	/**
	 * 导出Excel
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping("/m")  
	@ResponseBody
	public void export(HttpServletRequest request, HttpServletResponse response,String vendorsId,String releaseId) throws Exception {  
		String VendorsQuotationData = "报价表";
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setHeader("Content-Disposition", "attachment;filename="  
			        + new String(VendorsQuotationData.getBytes(),"iso-8859-1") + ".xlsx");
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象  
	        workbook = vendorsQuotationService.exportExcelInfo(VendorsQuotationData,vendorsId,releaseId);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	}
}
